package com.yfqy.app.modules.common;

import com.yfqy.app.domain.vo.ResultVO;
import com.yfqy.app.service.AliyunOSSService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/client/file")
public class ALYOSSController {

    @Resource
    private AliyunOSSService aliyunOSSService;


    /**
     * 上传图片到阿里云oss服务器
     *
     * @param file
     * @param folder
     * @return
     */
    @PostMapping("/upload2AliYunOSS")
    public ResultVO<String> upload(@RequestParam("file") MultipartFile file,
                                   @RequestParam(value = "folder", defaultValue = "images") String folder) {
        String url = aliyunOSSService.upload(file, folder);
        return ResultVO.data(url);
    }

    @PostMapping("/batchUpload2AliYunOSS")
    public ResultVO<List<String>> uploadBatch(@RequestParam("files") MultipartFile[] files,
                                              @RequestParam(value = "folder", defaultValue = "images") String folder) {
        List<String> urls = aliyunOSSService.uploadBatch(files, folder);
        return ResultVO.data(urls);
    }

    @PostMapping("/deleteAliYunOSS")
    public ResultVO<Void> delete(@RequestParam String url) {
        aliyunOSSService.delete(url);
        return ResultVO.success();
    }
}
